package cn.sunlight.mm.controller;

import cn.sunlight.framework.annotation.HmComponent;
import cn.sunlight.framework.annotation.HmRequestMapping;
import cn.sunlight.framework.annotation.HmSetter;
import cn.sunlight.mm.base.BaseController;
import cn.sunlight.mm.common.GlobalConst;
import cn.sunlight.mm.entity.PageResult;
import cn.sunlight.mm.entity.QueryPageBean;
import cn.sunlight.mm.entity.Result;
import cn.sunlight.mm.pojo.Course;
import cn.sunlight.mm.pojo.User;
import cn.sunlight.mm.security.HmAuthority;
import cn.sunlight.mm.service.CourseService;
import cn.sunlight.mm.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @author sunlight
 * @description: 学科控制器
 * @date 2021/5/19
 * @Version v1.0
 */
@Slf4j
@HmComponent
public class CourseController extends BaseController {

    @HmSetter("courseService")
    CourseService courseService;

    /**
     * 学科下拉列表初始化
     * @param request
     * @param response
     * @throws Exception
     */
    @HmAuthority("COURSE_LIST")
    @HmRequestMapping("/course/findListAll")
    public void findListForQuestion(HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            //无需处理前端数据,直接调用service
            List<Course> courseList=courseService.findListAll();

            printResult(response,new Result(true,"查询成功",courseList));
        }catch (RuntimeException e){
            e.printStackTrace();
            log.error("",e);
            printResult(response,new Result(false,"查询失败"+e.getMessage()));
        }
    }

    /**
     * 学科更新
     * @param request
     * @param response
     * @throws Exception
     */
    @HmRequestMapping("/course/update")
    public void update (HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            // 获取表单数据
            // 封装客户数据到POJO对象(id,name,isShow)
            Course course = parseJSON2Object(request, Course.class);
            log.debug("表单数据:{}",course);
            courseService.updateCourse(course);
            printResult(response,new Result(true,"更新成功"));
        }catch (RuntimeException e){
            e.printStackTrace();
            log.error("",e);
            printResult(response,new Result(false,"更新失败"+e.getMessage()));
        }
    }

    /**
     * 获取学科列表
     * @param request
     * @param response
     * @throws Exception
     */
    @HmRequestMapping("/course/findListByPage")
    public void findListByPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            QueryPageBean queryPageBean = parseJSON2Object(request, QueryPageBean.class);
            log.debug("queryPageBean:{}",queryPageBean);
            if (queryPageBean==null){
                queryPageBean=new QueryPageBean();
                queryPageBean.setCurrentPage(1);
                queryPageBean.setPageSize(10);
            }
            // 调用Service，获取PageResult
            //PageResult  pageResult = new PageResult(33L,new ArrayList());
            PageResult pageResult=courseService.findListByPage(queryPageBean);

            printResult(response,new Result(true,"获取成功",pageResult));
        }catch (RuntimeException e){
            e.printStackTrace();
            log.error("",e);
            printResult(response,new Result(false,"获取学科列表失败"+e.getMessage()));
        }
    }

    /**
     * 学科添加
     * @param request
     * @param response
     * @throws Exception
     */
    @HmRequestMapping("/course/add")
    public void addCourse (HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            //封装客户数据到pojo对象
            Course course = parseJSON2Object(request, Course.class);
            log.debug("表单数据：{}",course);
            //初始化表单数据
            course.setCreateDate(DateUtils.parseDate2String(new Date()));
            //获取用户ID
            User user = getSessionUser(request, GlobalConst.SESSION_KEY_USER);
            //为了调试,如果无Session,无用户，默认用户ID为1
            course.setUserId(user!=null?user.getId():1);
            log.debug("调用Service，完成业务 course:{}",course);

            courseService.addCourse(course);

            printResult(response,new Result(true,"新增成功"));
        }catch (RuntimeException e){
            e.printStackTrace();
            log.error("",e);
            printResult(response,new Result(false,"新增学科失败"+e.getMessage()));
        }
    }

}
